package kr.co.fashionmai.util;

public class PageUtil 
{
	private int totalCount;
	private int pageNo;
	
	private int firstPageNo;
	private int lastPageNo;
	
	private boolean hasPrevious;
	private boolean hasNext;
	
	private int viewRow; 
	
	
	public PageUtil(int pageNo, int totalCount, int viewRow)
	{
		this.pageNo = pageNo;
		this.totalCount = totalCount;
		this.viewRow = viewRow;
		
		makeFirstPageNo();
		makeLastPageNo();
	}

	public PageUtil(int pageNo, int totalCount)
	{
		this(pageNo, totalCount, 20);
	}
	  
	private void makeFirstPageNo()
	{
		this.firstPageNo = ((int) Math.ceil(pageNo/(double)10))*10-9 ;	// 1,11,21
		
		if(firstPageNo != 1)
		{
			this.hasPrevious = true;
		}
		this.lastPageNo = firstPageNo + (10-1);
	}
	
	private void makeLastPageNo()
	{
		int temp = (int) Math.ceil(this.totalCount/(double)this.viewRow);	//페이지보기할때 변수
	
		if(temp < lastPageNo)
		{
			this.lastPageNo = temp;
		}
		else if(temp == lastPageNo)
		{
			
		}
		else if(temp > lastPageNo)
		{
			this.hasNext = true;
		}
	}

	public String getPageStr()
	{
		String temp = "<a href='orderSheet.do?pageNo=:v1'>:v2</a>";
		StringBuffer buffer = new StringBuffer();
		
		if(this.hasPrevious)
		{
			buffer.append(temp.replaceAll(":v2", "<font size='3'> ◀  </font> &nbsp;&nbsp;").replaceAll(":v1", ""+(firstPageNo-1)));
		}
		for(int i=firstPageNo; i <= lastPageNo; i++)
		{
			if(pageNo == i)
			{
				buffer.append(temp.replaceAll(":v2", "<font size='4'> &nbsp;  <B>"+i + "&nbsp;</B></font>").replaceAll(":v1", ""+i));
				continue;
			}
			buffer.append(temp.replaceAll(":v2", "<font size='3'> &nbsp;  "+i + "&nbsp;</font>").replaceAll(":v1", ""+i));
		}
		if(hasNext)
		{
			buffer.append(temp.replaceAll(":v2", "&nbsp;<font size='3'> ▶ </font>").replaceAll(":v1", ""+(lastPageNo+1)));
		}
		return buffer.toString();
	}

}









